
from lxml import etree

import sys

t = etree.parse( sys.stdin )
n_lfc = 0

for pg in t.xpath( "/henry-output/result-inference/proofgraph" ):

	print "digraph {"

	obs_nodes = []
	other_nodes = []
	is_explained = {}
	
	for expl in pg.xpath( "./explanation" ):
		lhs, rhs = expl.text.split( "=>" )
		lhs, rhs = lhs.split( "^" ), rhs.split( "^" )

		for e in lhs+rhs:
			e						= e.split( ":" )
			nstr				= "n%s [shape=\"none\", label=\"%s:%.2f\", fontcolor=\"#000000\"]" % (e[1], e[0], float(e[3]) )
			
			if "2" == e[2]: obs_nodes += [nstr]
			if "3" == e[2]: other_nodes += [nstr]

		for explainee in rhs:
			explainee = explainee.split( ":" )
			is_explained[ explainee[1] ] = 1

			if len( lhs ) > 1:
				n_lfc += 1
				print "lfc%d [label=\"^\"]" % n_lfc
				print "lfc%d -> n%s" % (n_lfc, explainee[1])
			
			for explainer in lhs:
				explainer = explainer.split( ":" )

				if len( lhs ) > 1:
					print "n%s -> lfc%d" % (explainer[1], n_lfc)
				else:
					print "n%s -> n%s" % (explainer[1], explainee[1])

	def coloring( nodes ):
		return [n if is_explained.has_key( n.split( " " )[0][1:] ) else n.replace( "#000000", "#bb0000" ) for n in nodes]
	
	print "subgraph cluster_o {"
	print "\n".join( coloring( obs_nodes ) )
	print "}"

	print "\n".join( coloring( other_nodes ) )
	
	print "}"
